import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SQLserver {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //加载驱动包
        //连接
        DataSource dataSource= new  SQLServerDataSource();
        ((SQLServerDataSource)dataSource).setURL("jdbc:sqlserver://localhost:1433;databasename=stuman");
        ((SQLServerDataSource)dataSource).setUser("sa");
        ((SQLServerDataSource)dataSource).setPassword("zcwa");
        Connection connection=dataSource.getConnection();


        //构造SQL语句
        String s="select sno,sname from student ";
        PreparedStatement statement=connection.prepareStatement(s);

        //执行SQL语句
        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            String sno=resultSet.getString("sno");
            String sname=resultSet.getString("sname");
            System.out.println("学号:"+sno+" "+"名称:"+sname);
        }
        resultSet.close();
        statement.close();
        connection.close();




    }
}
